// pages/scan/addWine/addWine.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    styOpen:'',
    styClose:'styActive',
    // date:'2016-09-01',
    close:true,
    open:false,
    count:5,
    num:1,
    // day:[15,30],
    day1:15,
    day2:30,
    drinksNum:1,
    status:0,
    houseNum:0,
    active:false,
    ind:0,
    active2:'active',
    active3:'',
    dayNum:15
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  bindPickerChange: function (e) {
    let _this = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value;
    console.log(index)
    let ind = parseInt(index) - 1;
    let houseId, house_num;
    let houseIndex = _this.data.houseIndex;
    if (ind == -1) {
      houseId = -1,
        house_num = _this.data.hou[0]
      houseIndex = _this.data.hou[0]
    } else {
      houseId = _this.data.houses.house[ind].id;
      house_num = _this.data.houses.house[ind].name;
      houseIndex = _this.data.houses.house[ind].name
    }
    _this.setData({
      index: index,
      houseId: houseId,
      house_num: house_num,
      houseIndex: houseIndex,
      houNum: house_num
    })
  },
  bindPickerDay(e){
    let index = e.detail.value;
    console.log(index)
    this.setData({
      ind: index
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    let day = new Date();
    let date = day.toLocaleDateString();
    console.log(date)
    wx.request({
      url: app.globalData.baseUrl + '/api/house/house_num',
      data: {
        session_key: app.gs()
      },
      success(res) {
        let houses = res.data.data;
        let hous = []
        for (let i = 0; i < houses.house.length; i++) {
          hous[i] = houses.house[i].name
        }
        hous.unshift('选择房号')

        _this.setData({
          houses: houses,
          hous: hous,
          houseId: houses.house[0].id,
          is_scan: houses.num.is_scan,
          date: date,
          houNum: houses.num.house
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  houseNum(e){
    this.setData({
      houseNum:e.detail.value
    })
  },
 
  sureAddWine(e){
    let _this = this;
    _this.setData({
      cover:'active'
    })
    if (_this.data.houNum =='选择房号') {
      wx.showToast({
        title: '请输入当前房号',
      })
      return
    }
    if (_this.data.houNum == '选择房号') {
      wx.showToast({
        title: '请输入当前房号',
      })
      return
    }
    if (!_this.data.addName) {
      wx.showToast({
        title: '请输入存酒名称',
      })
      return
    }
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/drinks/access',
      data:{
        session_key:app.gs(),
        house_num: _this.data.houNum,
        date:_this.data.date,
        wine:_this.data.addName,
        status:_this.data.status,
        num:_this.data.drinksNum,
        day:_this.data.dayNum
      },
      success:function(res){
        console.log('resresresimg',res)
        wx.hideLoading()
        wx.navigateTo({
          url: 'successAddWine/successAddWine?img=' + res.data.data.code + '&wine_no=' + res.data.data.wine_no,
        })
      }
    })
    
  },
  bindDateChange (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      active:true
    })
  },
  closeOpen(e){
    let _this = this;
    // console.log(e)
    // console.log(e.currentTarget.id)
    if (e.currentTarget.id=="close"){
        _this.setData({
          styClose:'styActive',
          styOpen:'',
          close:true,
          open:false,
          // day:15,
          status:0,
          drinksNum:_this.data.num
        })
    }else{
      _this.setData({
        styClose: '',
        styOpen: 'styActive',
        close: false,
        open: true,
        // day:30,
        status: 1,
        drinksNum:_this.data.count/100
      })
    }
  },
  // dayNum(e){
  //   let _this = this;
  //   if(e.currentTarget.dataset.index==15){
  //     _this.setData({
  //       day:15
  //     })
  //   }else {
  //     _this.setData({
  //       day: 30
  //     })
  //   }
  // },

  addClose(e){
    let _this = this;
    console.log(e)
    if (e.currentTarget.id == "addClose") {
      let num = _this.data.num + 1;
      _this.setData({
        num: num,
        drinksNum: num
      })
    } else {
      let num = parseInt(_this.data.num);
      if (num > 1) {
        num = num - 1
      } else {
        num = num
      }
      _this.setData({
        num: num,
        drinksNum: num
      })
    }
  },
  addOpen(e){
    let _this = this;
    // console.log(e)
    // console.log(e.currentTarget.id)
    if (e.currentTarget.id == "addOpen") {
      let count = _this.data.count;
      if(count < 100){
        count = count + 5
      }else {
        count = count
      }
      _this.setData({
        count:count,
        drinksNum: count / 100
      })
    } else {
      
      let count = parseInt(_this.data.count);
      if (count > 5){
        count = count - 5
      }else {
        count = count
      }
      _this.setData({
        count : count,
        drinksNum:count / 100
      })
    }
  },
  addName(e){
    this.setData({
      addName:e.detail.value
    })
  },
  scanCode(e) {
    wx.scanCode({
      success(res) {
        console.log(res)
      }
    })
  },
  dayNum(e){
    console.log(e)
    let _this = this;
    let active2, active3;
    if (e.currentTarget.dataset.index =='15'){
        active2= 'active';
        active3= '';
      _this.setData({
          active2: active2,
          active3: active3,
          dayNum:15
        })
    }else {
      active3 = 'active';
      active2 = '';
     _this.setData({
        active2: active2,
        active3: active3,
        dayNum: 30
      })
    }
    
  },
  
  saoma(e) {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let scan = res.path;
        let path = scan.split('?')[0];
        //      分配房号-index
        if (path == 'pages/index/index') {
          let house_num = scan.match(/scene=(\S*)/)[1];

          wx.request({
            url: app.globalData.baseUrl + '/api/house/band',

            data: {
              session_key: app.gs(),
              house_num: house_num
            },
            success(res) {
              console.log('success', res)


              if (res.data.message == 200) {
                wx.showToast({
                  title: '确认完成'
                })
                // setTimeout(function () {
                //   wx.switchTab({
                //     url: '../../index/index'
                //   })
                // }, 2000)

                let day = new Date();
                let date = day.toLocaleDateString();
                console.log(date)
                wx.request({
                  url: app.globalData.baseUrl + '/api/house/house_num',
                  data: {
                    session_key: app.gs()
                  },
                  success(res) {
                    let houses = res.data.data;
                    let hous = []
                    for (let i = 0; i < houses.house.length; i++) {
                      hous[i] = houses.house[i].name
                    }
                    hous.unshift('选择房号')

                    _this.setData({
                      houses: houses,
                      hous: hous,
                      houseId: houses.house[0].id,
                      is_scan: houses.num.is_scan,
                      date: date,
                      houNum: houses.num.house
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  }
})